//
//  CAMediaTimingFunctionImageViewController.m
//  CoreAnimation
//
//  Created by 张国文 on 16/10/31.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "CAMediaTimingFunctionImageViewController.h"

@interface CAMediaTimingFunctionImageViewController ()

@property (nonatomic, strong) UIView *backgroundView;

@property (nonatomic, strong) UIView *whiteView;

@property (nonatomic, strong) CAShapeLayer *imageLayer;

@end

@implementation CAMediaTimingFunctionImageViewController

/**
 * CAMediaTimingFunction有一个叫做-getControlPointAtIndex:values:的方法，可以用来检索曲线的点，这个方法的设计的确有点奇怪（或许也就只有苹果能回答为什么不简单返回一个CGPoint），但是使用它我们可以找到标准缓冲函数的点，然后用UIBezierPath和CAShapeLayer来把它画出来。
 
    曲线的起始和终点始终是{0, 0}和{1, 1}，于是我们只需要检索曲线的第二个和第三个点（控制点）。
 */

/**
 *   ⚠️温馨提示：我发现- (void)getControlPointAtIndex:(size_t)idx values:(float[2])ptr 这个方法返回的值不是我们想要的，具体这个方法怎么使用我也不是太懂。因为运用这个方法得到的controlPoint1和controlPoint2经过贝塞尔重现都是一条直线。
 不过+ (instancetype)functionWithControlPoints:(float)c1x :(float)c1y :(float)c2x :(float)c2y;
 这个方法还是是很好用的，c1x和c1y就是第一个控制点，c2x和c2y是第二个控制点，比如：point1 = CGPointMake(0.1, 0.2), point2 = CGPointMake(0.5,0.6); 那么c1x = 0.1, c1y = 0.2, c2x = 0.5, c2y = 0.6，小窍门：如果运用这个方法创造自定义速度缓冲函数时候不知道具体的图形是什么，那么我们先可以用贝塞尔画出来，然后在使用相应的点来创造。

 */

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.backgroundView = [[UIView alloc] initWithFrame:self.view.frame];
    self.backgroundView.backgroundColor = [UIColor colorWithRed:150 / 255.0 green:150 / 255.0 blue:150 / 255.0 alpha:1];
    [self.view addSubview:_backgroundView];
    
    self.whiteView = [[UIView alloc] initWithFrame:CGRectMake(80, 80, 200, 200)];
    self.whiteView.backgroundColor = [UIColor whiteColor];
    self.whiteView.layer.geometryFlipped = YES;
    [self.backgroundView addSubview:_whiteView];
    
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame = CGRectMake(0, 300, 375, 30);
    button1.backgroundColor = [UIColor whiteColor];
    [button1 setTitle:@"kCAMediaTimingFunctionLinear图像" forState:UIControlStateNormal];
    [button1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    button1.tag = 100;
    [self.backgroundView addSubview:button1];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    button2.frame = CGRectMake(0, 350, 375, 30);
    button2.backgroundColor = [UIColor whiteColor];
    [button2 setTitle:@"kCAMediaTimingFunctionEaseIn图像" forState:UIControlStateNormal];
    [button2 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    button2.tag = 101;
    [self.backgroundView addSubview:button2];
    
    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
    button3.frame = CGRectMake(0, 400, 375, 30);
    button3.backgroundColor = [UIColor whiteColor];
    [button3 setTitle:@"kCAMediaTimingFunctionEaseOut图像" forState:UIControlStateNormal];
    [button3 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [button3 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    button3.tag = 102;
    [self.backgroundView addSubview:button3];
    
    UIButton *button4 = [UIButton buttonWithType:UIButtonTypeCustom];
    button4.frame = CGRectMake(0, 450, 375, 30);
    button4.backgroundColor = [UIColor whiteColor];
    [button4 setTitle:@"kCAMediaTimingFunctionEaseInEaseOut图像" forState:UIControlStateNormal];
    [button4 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [button4 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    button4.tag = 103;
    [self.backgroundView addSubview:button4];
    
    UIButton *button5 = [UIButton buttonWithType:UIButtonTypeCustom];
    button5.frame = CGRectMake(0, 500, 375, 30);
    button5.backgroundColor = [UIColor whiteColor];
    [button5 setTitle:@"kCAMediaTimingFunctionDefault图像" forState:UIControlStateNormal];
    [button5 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [button5 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    button5.tag = 104;
    [self.backgroundView addSubview:button5];
    
    UIButton *button6 = [UIButton buttonWithType:UIButtonTypeCustom];
    button6.frame = CGRectMake(0, 550, 375, 30);
    button6.backgroundColor = [UIColor whiteColor];
    [button6 setTitle:@"自定义MediaTimingFunction图像" forState:UIControlStateNormal];
    [button6 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [button6 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    button6.tag = 105;
    [self.backgroundView addSubview:button6];
    
}

- (void)click:(UIButton *)button
{
    if (self.imageLayer) {
        [self.imageLayer removeFromSuperlayer];
    }
    
    self.imageLayer = [CAShapeLayer layer];
    self.imageLayer.strokeColor = [UIColor redColor].CGColor;
    self.imageLayer.fillColor = [UIColor clearColor].CGColor;
    self.imageLayer.lineWidth = 3.0f;
    switch (button.tag) {
        case 100:
        {
            // kCAMediaTimingFunctionLinear
            CAMediaTimingFunction *timeFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
            CGPoint controlPoint1 = CGPointZero,controlPoint2 = CGPointZero;
            [timeFunction getControlPointAtIndex:1 values:(float *)&controlPoint1];
            [timeFunction getControlPointAtIndex:2 values:(float *)&controlPoint2];
            NSLog(@"controlPoint1 = %@, controlPoint2 = %@", [NSValue valueWithCGPoint:controlPoint1], [NSValue valueWithCGPoint:controlPoint2]);
            UIBezierPath *path = [[UIBezierPath alloc] init];
            [path moveToPoint:CGPointMake(0, 0)];
            [path addCurveToPoint:CGPointMake(1, 1) controlPoint1:controlPoint1 controlPoint2:controlPoint2];
            [path applyTransform:CGAffineTransformMakeScale(50, 50)];
          //  self.imageLayer.path = path.CGPath;
            
            self.whiteView.layer.contents = (__bridge id _Nullable)([UIImage imageWithName:@"kCAMediaTimingFunctionLinear"].CGImage);
            [self.whiteView.layer addSublayer:_imageLayer];
            break;
        }
            case 101:
        {
            // kCAMediaTimingFunctionEaseIn
            CAMediaTimingFunction *timeFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
            CGPoint controlPoint1 = CGPointZero,controlPoint2 = CGPointZero;
            [timeFunction getControlPointAtIndex:1 values:(float *)&controlPoint1];
            [timeFunction getControlPointAtIndex:2 values:(float *)&controlPoint2];
            NSLog(@"controlPoint1 = %@, controlPoint2 = %@", [NSValue valueWithCGPoint:controlPoint1], [NSValue valueWithCGPoint:controlPoint2]);
            UIBezierPath *path = [[UIBezierPath alloc] init];
            [path moveToPoint:CGPointMake(0, 0)];
            [path addCurveToPoint:CGPointMake(1, 1) controlPoint1:controlPoint1 controlPoint2:controlPoint2];
            [path applyTransform:CGAffineTransformMakeScale(50, 50)];
          //  self.imageLayer.path = path.CGPath;
            self.whiteView.layer.contents = (__bridge id _Nullable)([UIImage imageWithName:@"kCAMediaTimingFunctionEaseIn"].CGImage);
           [self.whiteView.layer addSublayer:_imageLayer];
            break;
        }
            case 102:
        {
            // kCAMediaTimingFunctionEaseOut
            CAMediaTimingFunction *timeFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
            CGPoint controlPoint1 = CGPointZero,controlPoint2 = CGPointZero;
            [timeFunction getControlPointAtIndex:1 values:(float *)&controlPoint1];
            [timeFunction getControlPointAtIndex:2 values:(float *)&controlPoint2];
            NSLog(@"controlPoint1 = %@, controlPoint2 = %@", [NSValue valueWithCGPoint:controlPoint1], [NSValue valueWithCGPoint:controlPoint2]);
            UIBezierPath *path = [[UIBezierPath alloc] init];
            [path moveToPoint:CGPointMake(0, 0)];
            [path addCurveToPoint:CGPointMake(1, 1) controlPoint1:controlPoint1 controlPoint2:controlPoint2];
            [path applyTransform:CGAffineTransformMakeScale(50, 50)];
      //      self.imageLayer.path = path.CGPath;
            self.whiteView.layer.contents = (__bridge id _Nullable)([UIImage imageWithName:@"kCAMediaTimingFunctionEaseOut"].CGImage);
            [self.whiteView.layer addSublayer:_imageLayer];
            break;
        }
            case 103:
        {
            // kCAMediaTimingFunctionEaseInEaseOut
            CAMediaTimingFunction *timeFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
            CGPoint controlPoint1 = CGPointZero,controlPoint2 = CGPointZero;            [timeFunction getControlPointAtIndex:1 values:(float *)&controlPoint1];
            [timeFunction getControlPointAtIndex:2 values:(float *)&controlPoint2];
            NSLog(@"controlPoint1 = %@, controlPoint2 = %@", [NSValue valueWithCGPoint:controlPoint1], [NSValue valueWithCGPoint:controlPoint2]);
            UIBezierPath *path = [[UIBezierPath alloc] init];
            [path moveToPoint:CGPointMake(0, 0)];
            [path addCurveToPoint:CGPointMake(1, 1) controlPoint1:controlPoint1 controlPoint2:controlPoint2];
            [path applyTransform:CGAffineTransformMakeScale(50, 50)];
        //    self.imageLayer.path = path.CGPath;
            self.whiteView.layer.contents = (__bridge id _Nullable)([UIImage imageWithName:@"kCAMediaTimingFunctionEaseInEaseOut"].CGImage);
           [self.whiteView.layer addSublayer:_imageLayer];
            break;
        }
            case 104:
        {
            // kCAMediaTimingFunctionDefault
            CAMediaTimingFunction *timeFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
            CGPoint controlPoint1 = CGPointZero,controlPoint2 = CGPointZero;
            [timeFunction getControlPointAtIndex:1 values:(float *)&controlPoint1];
            [timeFunction getControlPointAtIndex:2 values:(float *)&controlPoint2];
            NSLog(@"controlPoint1 = %@, controlPoint2 = %@", [NSValue valueWithCGPoint:controlPoint1], [NSValue valueWithCGPoint:controlPoint2]);
            UIBezierPath *path = [[UIBezierPath alloc] init];
            [path moveToPoint:CGPointMake(0, 0)];
            [path addCurveToPoint:CGPointMake(1, 1) controlPoint1:controlPoint1 controlPoint2:controlPoint2];
            [path applyTransform:CGAffineTransformMakeScale(50, 50)];
        //    self.imageLayer.path = path.CGPath;
            self.whiteView.layer.contents = (__bridge id _Nullable)([UIImage imageWithName:@"kCAMediaTimingFunctionDefault"].CGImage);
             [self.whiteView.layer addSublayer:_imageLayer];
            break;
        }
            case 105:
        {
            
//            CAMediaTimingFunction *timeFunction = [CAMediaTimingFunction functionWithControlPoints:1 :0 :0.75 :1];
//            CGPoint controlPoint1 = CGPointZero,controlPoint2 = CGPointZero;
//             NSLog(@"之前：controlPoint1 = %@, controlPoint2 = %@", [NSValue valueWithCGPoint:controlPoint1], [NSValue valueWithCGPoint:controlPoint2]);
//            [timeFunction getControlPointAtIndex:1 values:(float *)&controlPoint1];
//            [timeFunction getControlPointAtIndex:2 values:(float *)&controlPoint2];
//             NSLog(@"之后：controlPoint1 = %@, controlPoint2 = %@", [NSValue valueWithCGPoint:controlPoint1], [NSValue valueWithCGPoint:controlPoint2]);
            
            UIBezierPath *path = [[UIBezierPath alloc] init];
            [path moveToPoint:CGPointMake(0, 0)];
            [path addCurveToPoint:CGPointMake(1, 1) controlPoint1:CGPointMake(1, 0) controlPoint2:CGPointMake(0.75, 1)];
            [path applyTransform:CGAffineTransformMakeScale(200, 200)];
            self.imageLayer.path = path.CGPath;
            [self.whiteView.layer addSublayer:_imageLayer];
            break;
        }
        default:
            break;
    }

}



@end
